package version1;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class SubmitFile extends JFrame implements ActionListener {

	private CondorHandler ch = new CondorHandler();
	private File execFile, subFile;

	JPanel panel1 = new JPanel();

	JLabel emptyLabel = new JLabel("");
	JRadioButton subFileRadio = new JRadioButton("Use existing submit file: ");
	JRadioButton manualRadio = new JRadioButton("Or use following settings: ");
	JLabel submitFileLabel = new JLabel("Submit file: ");
	JLabel folderLabel = new JLabel("File in filesystem: ");
	JLabel parameterLabel = new JLabel("Parameters seperated with , ");
	JTextField parameterText = new JTextField();
	JLabel universeLabel = new JLabel("Universe:");
	JTextField universeText = new JTextField();
	JLabel outputLabel = new JLabel("Output file(Optional):");
	JTextField outputText = new JTextField();
	JLabel inputLabel = new JLabel("Input file(Optional):");
	JTextField inputText = new JTextField();
	JButton cancelButton = new JButton("Cancel");
	JButton fileButton = new JButton("Open File");
	JButton subFileButton = new JButton("Open File");
	JButton submitButton = new JButton("Submit");

	//File explorer object 	
	JFileChooser fc;

	SubmitFile(){

		//Group buttons
		ButtonGroup group = new ButtonGroup();
		group.add(subFileRadio);
		group.add(manualRadio);
		
		subFileRadio.setSelected(true);
		setSubFileButtonSelected(true);

		//Add panels
		panel1.setLayout(new GridLayout(8,2));
		add(panel1);
		panel1.add(subFileRadio);
		panel1.add(subFileButton);
		panel1.add(manualRadio);
		panel1.add(emptyLabel);
		panel1.add(folderLabel);
		panel1.add(fileButton);
		panel1.add(parameterLabel);
		panel1.add(parameterText);
		panel1.add(universeLabel);
		panel1.add(universeText);
		panel1.add(inputLabel);
		panel1.add(inputText);
		panel1.add(outputLabel);
		panel1.add(outputText);
		panel1.add(cancelButton);
		panel1.add(submitButton);

		//Center of screen
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);

		//Action Listener
		submitButton.addActionListener(this);
		cancelButton.addActionListener(this);
		fileButton.addActionListener(this);
		subFileButton.addActionListener(this);
		subFileRadio.addActionListener(this);
		manualRadio.addActionListener(this);

		pack();
		setVisible(true);
		setSize(400,200);
		fc = new JFileChooser();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == cancelButton){
			dispose();
		} else if (e.getSource() == fileButton) {
			int returnVal = fc.showOpenDialog(this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				execFile = fc.getSelectedFile();
			}
		} else if (e.getSource() == subFileButton) {
			int returnVal = fc.showOpenDialog(this);

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				subFile = fc.getSelectedFile();
			}
		} else if (e.getSource() == subFileRadio) {
			setSubFileButtonSelected(true);
		} else if (e.getSource() == manualRadio) {
			setSubFileButtonSelected(false);
		} else if (e.getSource() == submitButton) {
			if (subFileRadio.isSelected()) {
				ch.submitJob(subFile.getAbsolutePath());
			} else {
				ch.submitJob(execFile.getAbsolutePath(), universeText.getText(), 
					parameterText.getText(), outputText.getText(), inputText.getText());
			}
			dispose();
		}
	}
	
	private void setSubFileButtonSelected(boolean b) {
		subFileButton.setEnabled(b);
		folderLabel.setEnabled(!b); fileButton.setEnabled(!b);
		parameterLabel.setEnabled(!b); parameterText.setEnabled(!b);
		universeLabel.setEnabled(!b); universeText.setEnabled(!b);
		inputLabel.setEnabled(!b); inputText.setEnabled(!b);
		outputLabel.setEnabled(!b); outputText.setEnabled(!b);
	}
	
}